package com.znufe.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.znufe.dao.UserDao;
import com.znufe.model.User;

@Repository("UserDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
	
	@Autowired
	public UserDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	public void addUser(User user){
		getHibernateTemplate().save(user);
	}

	public void deleteUser(User user){
		getHibernateTemplate().delete(user);
	}
	
	public void updateUser(User user){
		getHibernateTemplate().update(user);
	}
	
	public User findUserById(Integer id){
		return (User)getHibernateTemplate().get(User.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findAllUser(){
		return (List<User>)getHibernateTemplate().find("from User user order by user.id DESC");
	}

	
	@SuppressWarnings("unchecked")
	public User checkUser(User user) {
		List<User> users = (List<User>)getHibernateTemplate().find("from User where name= '" + user.getName() + "' " +
				"and password = '" + user.getPassword() + "'");
		if(users.size() == 0)
			return null;
		else return users.get(0);
	}

}
